package com.school.service.impl;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.school.dao.HljtGradeClassDao;
import com.school.entity.HljtClassimage;
import com.school.entity.web.HljtClassInfo;
import com.school.entity.web.HljtGradeInfo;
import com.school.service.HljtGradeClassService;
import com.school.util.MapperToJson;
import com.school.util.RepMessage;
import com.school.util.WhetherNotToUpdate;
@Service
public class HljtGradeClassServiceImpl  implements HljtGradeClassService{

	@Autowired
	private HljtGradeClassDao oHljtGradeClassDao;
	
	@Override
	public String findAllGrades() {
		List<HljtGradeInfo> list=oHljtGradeClassDao.findAllGrades();
		//List<HljtGradeInfo> gList=new ArrayList<HljtGradeInfo>();
		for (HljtGradeInfo hljtGradeInfo : list) {
			int nCount=oHljtGradeClassDao.selectClassCountByGrade(hljtGradeInfo.getGrade());
//			HljtGradeInfo oHljtGradeInfo=new HljtGradeInfo();
//			oHljtGradeInfo.setGrade(string);
			hljtGradeInfo.setClassNums(nCount);
//			oHljtGradeInfo.setClassNums(nCount);
//			gList.add(oHljtGradeInfo);
		}
		return 	MapperToJson.toJson(list);
		}

	@Override
	public String insertGrade(String strGrade) {RepMessage oRepMessage=new RepMessage();
	HljtGradeInfo oHljtGradeInfo=	oHljtGradeClassDao.selectGrades(strGrade);
	if(oHljtGradeInfo == null || "".equals(oHljtGradeInfo)){
		
		int nCount=oHljtGradeClassDao.insertGrade(strGrade);
		if(nCount!=0){
			WhetherNotToUpdate.setPattern(true);
			oRepMessage.setFlag(true);
		}
	return MapperToJson.toJson(oRepMessage);
	}else{
		oRepMessage.setFlag(false);
		oRepMessage.setErrInfo("已存在年级");
	}
	
	return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String updateGrade(HljtGradeInfo oHljtGradeInfo) {
		RepMessage oRepMessage=new RepMessage();
		HljtGradeInfo nHljtGradeInfo=oHljtGradeClassDao.selectGrades(oHljtGradeInfo.getGrade());
		if(nHljtGradeInfo == null || "".equals(nHljtGradeInfo)){
			oHljtGradeClassDao.updateGrade(oHljtGradeInfo);
			HljtClassInfo oHljtClassInfo=new HljtClassInfo();
			oHljtClassInfo.setGrade(oHljtGradeInfo.getGrade());
			oHljtGradeClassDao.updateClassInfo(oHljtClassInfo);
			oRepMessage.setFlag(true);
			WhetherNotToUpdate.setPattern(true);
		}else{
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("年级重复");
		}
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String deleteGrade(HljtGradeInfo oHljtGradeInfo) {
		RepMessage oRepMessage=new RepMessage();
		int nCount=	oHljtGradeClassDao.selectClassCountByGrade(oHljtGradeInfo.getGrade());
		
		if(nCount!=0){
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("此年级下还有班级信息");
		}else{
			oHljtGradeClassDao.deleteGrade(oHljtGradeInfo.getId());
			WhetherNotToUpdate.setPattern(true);
			oRepMessage.setFlag(true);
		}
		return MapperToJson.toJson(oRepMessage);
	}
	
	
	
	public Map<String , Object> findAllClassesEX(HljtClassInfo hljtClassInfo){
		Map<String , Object> map = new HashMap<>();
		List<HljtClassInfo> temp = oHljtGradeClassDao.findAllClassInfoEX(hljtClassInfo);
		for (HljtClassInfo hl : temp) {
			try {
				if(hl.getIsexcellent().equals("2")) {
					hl.setIsexcellent("false");
				}else if(hl.getIsexcellent().equals("1")) {
					hl.setIsexcellent("true");
				}
				hl.setImagePath(WhetherNotToUpdate.getIp()+hl.getImagePath());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		map.put("info", temp);
		map.put("total", oHljtGradeClassDao.findAllClassCountEX(hljtClassInfo));
		return map;
	}
	
	
	
	/**
	 * 
	 * 班级内容
	 * @param pageIndex
	 * @param pageSize
	 * @param strGrade
	 * @param strClass
	 * @return
	 */
	public String findAllClasses(int pageIndex,int pageSize,String strGrade,String strClass ,String strPath){
	int flag=0;
	if(strClass=="true"){
		flag=1;
	}
	List<HljtClassInfo> list=new ArrayList<HljtClassInfo>();
	int nCount=0;
	if(strGrade=="" & strClass==""){
		nCount=oHljtGradeClassDao.selectClassCount();
		list=oHljtGradeClassDao.findAllClassInfo(pageIndex, pageSize);
	}else if(strGrade!=""& strClass==""){
		nCount=oHljtGradeClassDao.selectClassCountByGrade(strGrade);
		list=oHljtGradeClassDao.findClassInfoByGrade(pageIndex, pageSize, strGrade);
	}else if(strGrade=="" & strClass!=""){
		nCount=oHljtGradeClassDao.selectClassCountByClassInfo(flag);
		list=oHljtGradeClassDao.findClassInfoByClassInfo(pageIndex, pageSize, flag);
	}else if(strGrade!="" & strClass!=""){
		nCount=oHljtGradeClassDao.selectClassCountByGradeAndClassInfo(strGrade, flag);
		list=oHljtGradeClassDao.findClassInfoByClassInfoAndGrade(pageIndex, pageSize, strGrade, flag);
	}
	for (HljtClassInfo hljtClassInfo : list) {
		hljtClassInfo.setIsexcellent(MapperToJson.intToBoolean(hljtClassInfo.getIsexcellent()));
		hljtClassInfo.setImagePath(strPath+"/images/schools/"+hljtClassInfo.getImagePath());
	}
	RepMessage oRepMessage=new RepMessage();
	oRepMessage.setFlag(true);
	HashMap<String,Object> oMap= new HashMap<String,Object>();
	oMap.put("Sums", nCount);
	oMap.put("data",list);
	oRepMessage.setObjData(oMap);
	return MapperToJson.toJson(oRepMessage);
	}
	/**
	 * 添加班级信息
	 * @param oHljtClassInfo
	 * @return
	 */
	public String insertClassInfo(HljtClassInfo oHljtClassInfo){
		HljtClassInfo tHljtClassInfo=oHljtGradeClassDao.selectClassInfoByGradeAndClasses(oHljtClassInfo.getGrade(),oHljtClassInfo.getClasses());
		RepMessage oRepMessage=new RepMessage();
		if(tHljtClassInfo == null || "".equals(tHljtClassInfo)){
			//oHljtClassInfo.setGradeid(Integer.parseInt(oHljtGradeClassDao.selectGrades(oHljtClassInfo.getGrade()).getId()));
			oHljtClassInfo.setClassid(oHljtClassInfo.getId());
			oHljtClassInfo.setIsexcellent(MapperToJson.booleanToInt(oHljtClassInfo.getIsexcellent()));
			int nCount=	oHljtGradeClassDao.insertClassInfo(oHljtClassInfo);
			oHljtGradeClassDao.insertClassId(oHljtClassInfo.getId());
			if(nCount!=0){
				HljtClassimage oHljtClassimage=new HljtClassimage();
				oHljtClassimage.setClassid(oHljtClassInfo.getId());
				oHljtClassimage.setTitle("优秀班级");
				oHljtClassimage.setType("2");
				oHljtClassimage.setImagepath(oHljtClassInfo.getImagePath());
				int nums=oHljtGradeClassDao.insertClassImage(oHljtClassimage);
				if(nums!=0){
					oRepMessage.setFlag(true);
				}else{
					oRepMessage.setFlag(false);
				}
			}else{
				oRepMessage.setFlag(false);
			}
		}else{
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("班级已存在");
		}
		return MapperToJson.toJson(oRepMessage);
	}
	
	public String updateClassInfo(HljtClassInfo oHljtClassInfo){
		RepMessage oRepMessage=new RepMessage();
	//	HljtClassInfo d = oHljtGradeClassDao.selectClassInfoByGradeAndClasses(oHljtClassInfo.getGrade(),oHljtClassInfo.getClasses());
		
	//	if(d !=null && d.getClasses().equals(oHljtClassInfo.getClasses()) && d.getGrade().equals(oHljtClassInfo.getGrade())) {
		//	System.out.println("tttt");
		//	oRepMessage.setFlag(false);
	//	}else {
			oHljtClassInfo.setIsexcellent(MapperToJson.booleanToInt(oHljtClassInfo.getIsexcellent()));
			int nCount=oHljtGradeClassDao.updateClassInfo(oHljtClassInfo);
			
			if(nCount!=0){
				oRepMessage.setFlag(true);
			}else{
				oRepMessage.setFlag(false);
			}
	//	}
		return MapperToJson.toJson(oRepMessage);
	}
	
	public String deleteClassInfo(String strId){
		int nCount=oHljtGradeClassDao.deleteClassInfo(strId);
		RepMessage oRepMessage=new RepMessage();
		if(nCount!=0){
			oRepMessage.setFlag(true);
		}else{
			oRepMessage.setFlag(false);
		}
		return MapperToJson.toJson(oRepMessage);
	}
}
